package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.Task;
import com.squareup.picasso.Picasso;

import java.util.List;

public class TaskReviewAdapter extends RecyclerView.Adapter<TaskReviewAdapter.TaskViewHolder> {

    private Context context;
    private List<Task> tasks;
    private TaskReviewListener listener;

    public interface TaskReviewListener {
        void onTaskSelected(Task task);
    }

    public TaskReviewAdapter(Context context, List<Task> tasks, TaskReviewListener listener) {
        this.context = context;
        this.tasks = tasks;
        this.listener = listener;
    }

    @NonNull
    @Override
    public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_task_review, parent, false);
        return new TaskViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
        Task task = tasks.get(position);
        holder.bind(task);
    }

    @Override
    public int getItemCount() {
        return tasks.size();
    }

    class TaskViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvDescription;
        TextView tvUsername;
        TextView tvReward;
        TextView tvTaskStatus;
        ImageView ivTaskImage;
        Button btnViewDetails;
        Button btnApprove;
        Button btnReject;

        TaskViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_task_title);
            tvDescription = itemView.findViewById(R.id.tv_task_description);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvReward = itemView.findViewById(R.id.tv_reward);
            tvTaskStatus = itemView.findViewById(R.id.tv_task_status);
            ivTaskImage = itemView.findViewById(R.id.iv_task_image);
            btnViewDetails = itemView.findViewById(R.id.btn_view_task);
            btnApprove = itemView.findViewById(R.id.btn_approve);
            btnReject = itemView.findViewById(R.id.btn_reject);
        }

        void bind(Task task) {
            tvTitle.setText(task.getTitle());
            tvDescription.setText(task.getDescription());
            tvReward.setText(String.format("奖励：%.2f元", task.getReward()));
            tvTaskStatus.setText(getStatusText(task.getStatus()));
            
            // 设置任务图片
            if (task.getImageUrl() != null && !task.getImageUrl().isEmpty()) {
                ivTaskImage.setVisibility(View.VISIBLE);
                Picasso.get().load(task.getImageUrl())
                        .placeholder(R.drawable.placeholder_image)
                        .error(R.drawable.error_image)
                        .into(ivTaskImage);
            } else {
                ivTaskImage.setVisibility(View.GONE);
            }
            
            // 设置点击事件
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onTaskSelected(task);
                }
            });
            
            btnViewDetails.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onTaskSelected(task);
                }
            });
        }
        
        private String getStatusText(String status) {
            switch (status) {
                case Task.STATUS_PENDING:
                    return "待审核";
                case Task.STATUS_APPROVED:
                    return "已通过";
                case Task.STATUS_REJECTED:
                    return "已拒绝";
                case Task.STATUS_COMPLETED:
                    return "已完成";
                case Task.STATUS_FAILED:
                    return "失败";
                default:
                    return status;
            }
        }
    }
} 